package com.zian.controller.admin;


import cn.hutool.core.lang.tree.Tree;
import com.zian.annotation.OperLog;
import com.zian.common.R;
import com.zian.domain.dto.CategoryDTO;

import com.zian.domain.po.SysGoodsCategory;
import com.zian.domain.vo.CategoryVO;
import com.zian.enums.BusinessType;
import com.zian.service.ISysGoodsCategoryService;
import com.zian.validation.ValidGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author
 * @since 2024-09-08
 */
@Slf4j
@Api("商品分类接口")
@RestController("SysGoodsCategoryControllerAdmin")
@RequestMapping("/admin/categorys")
@RequiredArgsConstructor
public class SysGoodsCategoryController {


    private final ISysGoodsCategoryService goodsCategoryService;

    @GetMapping("/list")
    @ApiOperation("查询商品分类")
    public R<List<Tree<Long>>> queryCategory() {
        log.info("查询商品分类");
        return R.ok(goodsCategoryService.queryCategory());
    }

    @PostMapping
    @ApiOperation("新增商品分类")
    @OperLog(title = "商品分类", businessType = BusinessType.INSERT)
    public R addCategory(@RequestBody @Validated(ValidGroup.Crud.Create.class) CategoryDTO dto) {
        log.info("新增商品分类：{}", dto);
        goodsCategoryService.addCategory(dto);
        return R.ok();
    }

    @PutMapping
    @ApiOperation("修改商品分类")
    @OperLog(title = "商品分类", businessType = BusinessType.UPDATE)
    public R updateCategory(@RequestBody @Validated(ValidGroup.Crud.Update.class) CategoryDTO dto) {
        log.info("修改商品分类：{}", dto);
        goodsCategoryService.updateCategory(dto);
        return R.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除商品分类")
    @OperLog(title = "商品分类", businessType = BusinessType.DELETE)
    public R deleteCategoryById(@PathVariable Long id, Boolean flag) {
        log.info("删除商品分类：{}", id);
        goodsCategoryService.deleteCategoryById(id, flag);
        return R.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation("查询商品分类")
    public R<SysGoodsCategory> getCategoryById(@PathVariable Long id) {
        log.info("查询商品分类：{}", id);
        return R.ok(goodsCategoryService.getCategoryById(id));
    }

    // 根据父级id获取子分类
    @GetMapping("/children/{id}")
    @ApiOperation("根据父级id获取子分类")
    public R<List<Tree<Long>>> getChildrenCategory(@PathVariable Long id) {
        log.info("根据父级id获取子分类：{}", id);
        List<Tree<Long>> list = goodsCategoryService.getChildrenCategory(id);
        return R.ok(list);
    }


    // 获取所有顶级分类
    @GetMapping("/top")
    @ApiOperation("获取所有顶级分类")
    public R<List<CategoryVO>> getTopCategory() {
        log.info("获取所有顶级分类");
        List<CategoryVO> list = goodsCategoryService.getTopCategory();
        return R.ok(list);
    }

}
